/** The following trigger will populate and maintain SPA Account Junction table
*      
*      Author - Manu Dhingra
*      Company - Cognizant Technology Solutions
*      Client - Cisco (LinkSys)
*      Created - Feb 2010
*      Last update - March 2010
* 
**/

trigger SpaJunctions on SPA__c (after insert, after update) {

list<SpaAccountJunction__c> addAccList = new list<SpaAccountJunction__c>();
list<SpaContactJunction__c> addConList = new list<SpaContactJunction__c>();

    for (SPA__c n_spa : Trigger.new){
// If trigger is caused by tUpdate action.          
        if(Trigger.isUpdate){
        	for (SPA__c o_spa : Trigger.old){
    			if (n_spa.UtilFlag__c != o_spa.UtilFlag__c) return;
    			}
        	
            // Delete all exisitng mapping from Junction Tables
            SpaAccountJunction__c[] delAccList = [SELECT ID FROM SpaAccountJunction__c a
                                                    WHERE a.SPA_Id__c = :n_spa.id ];

            SpaContactJunction__c[] delConList = [SELECT ID FROM SpaContactJunction__c c
                                                    WHERE c.SPA_Id__c = :n_spa.id ];
            delete delAccList;
            delete delConList;
            }
            // Check if the Account or Contact Fields are not null tham create relation in Junction Tables
            if (n_spa.Distributor__c != null)
                addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Distributor__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Distributor2__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Distributor2__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Distributor3__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Distributor3__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Distributor4__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Distributor4__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer1__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Customer1__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer2__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Customer2__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer3__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Customer3__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer4__c != null)
            addAccList.add(new SpaAccountJunction__c (Account_Id__c = n_spa.Customer4__c , SPA_Id__c = n_spa.ID));

            if (n_spa.Customer_Contact1__c != null)
                addConList.add(new SpaContactJunction__c (Contact_Id__c = n_spa.Customer_Contact1__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer_Contact2__c != null)
                addConList.add(new SpaContactJunction__c (Contact_Id__c = n_spa.Customer_Contact2__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer_Contact3__c != null)
                addConList.add(new SpaContactJunction__c (Contact_Id__c = n_spa.Customer_Contact3__c , SPA_Id__c = n_spa.ID));
            if (n_spa.Customer_Contact4__c != null)
                addConList.add(new SpaContactJunction__c (Contact_Id__c = n_spa.Customer_Contact4__c , SPA_Id__c = n_spa.ID));
        }           

    insert addAccList;
    insert addConList;
}